<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require './application/controllers/pc_controller.php';

class Widget extends PC_controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('Projectmodel','m_project');
		$this->load->model('Widgetmodel','m_widget');
		$this->load->model('Templatemodel','m_template');
		$this->load->model('Codemodel','m_code');
	}

	public function index(){
		$data = parent::index();
		$data['attr'] = array(
			'project_list' => $this->m_project->outputProjectList4Js(),
		);
		$this->load->view('widget',$data);
	}

	public function getWidgetList(){
		$assist['where'] = getFilter($this->input->post(),Widgetmodel::$searchField);
		$assist['like'] = getFilter($this->input->post(),Widgetmodel::$likeField);
		$assist['sort'] = getSort($this->input->post(),Widgetmodel::$sortField);
		$assist['pagination'] = getPagination($this->input->post());
		$project_ids = $this->m_project->getActiveProjectIdList();
		list($list,$count) = $this->m_widget->getWidgetListSection($project_ids,$assist);

		

		$users = extractColumn($list,'user_id');
		$users = $this->m_user->getUserById($users);
		$users = reindexArray($users,'user_id');
		foreach ($list as &$record) {
			$record['user_name'] = '';
			if(isset($users[$record['user_id']])) $record['user_name'] = $users[$record['user_id']]['user_name'];
			$record['template_count'] = $this->m_template->getAllTemplateWidgetListByProjectID($record['project_id'],$record['widget_name']);
		}

		$this->response->setSuccess();
		$this->response->setItems($list);
		$this->response->setTotalCount($count);
		$this->response->responseExit();
	}

	public function createWidget(){
		$this->validate->setRule('project_id','integer');
		$this->validate->setRule('project_id','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('widget_name','string');
		$this->validate->setRule('widget_name','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		if(!$this->m_widget->checkNameAvailable($info['widget_name'],$info['project_id'])){
			
			$this->response->setDetail($this->lang->line('error_project_name_used'));
			$this->response->responseExit();
		}

		$info['user_id'] = $this->m_app->getCurrentUserId();
		$info['widget_status'] = STATUS_ACTIVE;
		$info['widget_css'] = ".w-".$info['widget_name']." {\n\n}";
		$info['widget_html'] = "<!-- WIDGET-".$info['widget_name']."-BOF -->\n<div class=\"w-".$info['widget_name']."\">\n</div>\n<!-- WIDGET-".$info['widget_name']."-EOF -->";
		$info['widget_time_lastmodified'] = NOW;
		$widget_id = $this->m_widget->createWidget($info);
		
		//backup
		$this->m_widget->createWidgetHistory(array(
			'widget_id' => $widget_id,
			'user_id' => $this->m_app->getCurrentUserId(),
			'widget_history_html' => $info['widget_html'],
			'widget_history_time_create' => NOW,
		));



		$this->response->setSuccess();
		$this->response->setAddon(array('project_id'=>$info['project_id']));
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function copyWidget(){
		$this->validate->setRule('widget_id','integer');
		$this->validate->setRule('widget_id','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('project_id','integer');
		$this->validate->setRule('project_id','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('widget_name','string');
		$this->validate->setRule('widget_name','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		if(!$this->m_widget->checkNameAvailable($info['widget_name'],$info['project_id'])){
			
			$this->response->setDetail($this->lang->line('error_project_name_used'));
			$this->response->responseExit();
		}
		$copy_widget = $this->m_widget->getWidget($info['widget_id']);

		$copy_widget['project_id'] = $info['project_id'];
		$copy_widget['widget_html'] = str_replace($copy_widget['widget_name'], $info['widget_name'], $copy_widget['widget_html']);
		$copy_widget['widget_css'] = str_replace($copy_widget['widget_name'], $info['widget_name'], $copy_widget['widget_css']);
		$copy_widget['widget_name'] = $info['widget_name'];
		$copy_widget['user_id'] = $this->m_app->getCurrentUserId();
		$copy_widget['widget_status'] = STATUS_ACTIVE;
		$info['widget_time_lastmodified'] = NOW;
		unset($copy_widget['widget_id']);
		$widget_id = $this->m_widget->createWidget($copy_widget);
		$info = array(
			'widget_id' => $widget_id,
			'user_id' => $this->m_app->getCurrentUserId(),
			'widget_history_html' => $copy_widget['widget_html'],
			'widget_history_time_create' => NOW,
		);
		$this->m_widget->createWidgetHistory($info);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function editWidgetBaseInfo(){
		$this->validate->setRule('widget_id','integer');
		$this->validate->setRule('widget_id','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('project_id','integer');
		$this->validate->setRule('project_id','optional');
		$this->validate->setRule('project_id','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('widget_name','string');
		$this->validate->setRule('widget_name','optional');
		$this->validate->setRule('widget_name','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		if(!$this->m_widget->checkNameAvailable($info['widget_name'],$info['project_id'],$info['widget_id'])){
			
			$this->response->setDetail($this->lang->line('error_project_name_used'));
			$this->response->responseExit();
		}

		$info['user_id'] = $this->m_app->getCurrentUserId();
		$info['widget_time_lastmodified'] = NOW;
		$this->m_widget->editWidget($info['widget_id'],$info);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function deleteWidget(){
		$this->validate->setRule('widget_id','integer');
		$this->validate->setRule('widget_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$this->m_widget->editWidget($info['widget_id'],array('widget_status'=>STATUS_DELETE));

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function getWidgetTemplateList(){
		$this->validate->setRule('widget_name','string');
		$this->validate->setRule('widget_name','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('project_id','integer');
		$this->validate->setRule('project_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$assist['pagination'] = getPagination($this->input->post());
		list($list,$count) = $this->m_widget->getWidgetTemplateListSection($info['widget_name'],$info['project_id'],$assist);

		$this->response->setSuccess();
		$this->response->setItems($list);
		$this->response->setTotalCount($count);
		$this->response->responseExit();
	}
}
/* End of file widget.php */
/* Location: ./application/controllers/widget.php */